package com.yy.wga.goods.service.task;

import com.yy.wga.common.core.dto.BaseRequestDTO;
import com.yy.wga.common.core.dto.Result;
import com.yy.wga.common.core.exception.BusinessException;
import com.yy.wga.common.util.common.StringUtils;
import com.yy.wga.common.web.intereceptor.RequestHolder;
import com.yy.wga.goods.service.api.response.bind.GetPromoterRegisterAndBindStatusResp;
import com.yy.wga.goods.service.service.PromoterService;
import com.yy.wga.merchant.feign.api.MerchantInfoFeign;
import com.yy.wga.merchant.feign.response.MerchantMpConfigInfoResponse;
import com.yy.wga.user.feign.api.UserWechatFeign;
import com.yy.wga.user.feign.request.UserWechatListRequest;
import com.yy.wga.user.feign.request.UserWechatSaveRequest;
import com.yy.wga.user.feign.response.UserWechatInfoResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@RequiredArgsConstructor
@Component
public class SyncWechatPromoteSharerAppIdTask {

    @Resource
    private UserWechatFeign wechatFeign;
    @Resource
    private MerchantInfoFeign merchantInfoFeign;
    @Resource
    private PromoterService promoterService;

//    @Scheduled(cron = "0 0/30 * * * ? ")
    public void syncWechatPromoteSharerAppIdTask() {
        Result<List<MerchantMpConfigInfoResponse>> result = merchantInfoFeign.list();
        if (!result.isSuccess()) {
            log.error("读取商户配置异常");
            return;
        }
        List<MerchantMpConfigInfoResponse> merchantInfoDOList = result.getData();
        for (MerchantMpConfigInfoResponse merchantInfoDO : merchantInfoDOList) {

            String appKey = merchantInfoDO.getAppKey();
            BaseRequestDTO baseRequestDTO = new BaseRequestDTO();
            baseRequestDTO.setAppKey(appKey);
            RequestHolder.setBaseRequest(baseRequestDTO);

            UserWechatListRequest userWechatListRequest = new UserWechatListRequest();
            userWechatListRequest.setSharerAppid("");
            Result<List<UserWechatInfoResponse>> wechatInfoResult = wechatFeign.list(userWechatListRequest);
            if (!wechatInfoResult.isSuccess()) {
                throw new BusinessException("获取用户信息失败");
            }
            List<UserWechatInfoResponse> wechatDOList = wechatInfoResult.getData();
            if (wechatDOList.isEmpty()) {
                throw new BusinessException("未查询到用户");
            }
            for (UserWechatInfoResponse userWechat : wechatDOList) {
                if (StringUtils.isBlank(userWechat.getSharerAppid())) {
                    GetPromoterRegisterAndBindStatusResp statusResp = promoterService.getPromoterRegisterAndBindStatus(userWechat.getOpenId());
                    String sharerAppid = statusResp.getSharerAppid();
                    UserWechatSaveRequest updateRequest = new UserWechatSaveRequest();
                    updateRequest.setUserId(userWechat.getUserId());
                    updateRequest.setSharerAppid(sharerAppid);
                    wechatFeign.save(updateRequest);
                }
            }
            RequestHolder.removeBaseRequest();
        }
    }

}
